package satirGame;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;

public class MapNode extends JToggleButton {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4799679648005311370L;
	private int pIndex;//xPos, yPos is useless can delete
	private Image backgroundImage;
	private static final Dimension nodeSize=new Dimension(80,80);
	public MapNode(int x, int y, int t, int p){
		pIndex=p;

		setBackgroundImage(t);

		setPreferredSize(nodeSize);
		setSelected(false);
		
	}
	
	public int getIndex(){
		return pIndex;
	}
	
	public void setIconState(ImageIcon nodeIcon){
		setIcon(nodeIcon);
		revalidate();
	}
	
	public void setBackgroundImage(int t )
    {
		Toolkit tk = Toolkit.getDefaultToolkit();  
		Image nodebg=tk.getImage("img/"+String.valueOf(t)+".jpg");
        MediaTracker mt = new MediaTracker( this );
        mt.addImage( nodebg, 0 );
        try
        {
            mt.waitForAll();
            backgroundImage = nodebg;
        }
        catch( InterruptedException x )
        {
            System.err.println(
                "Specified background image could not be loaded." );
        }
        revalidate();
    }
 
    public void paintComponent( Graphics g )
    {
        super.paintComponent( g );
 
        Color saved = g.getColor();
        g.setColor( getBackground() );
        g.fillRect( 0, 0, getWidth(), getHeight() );
        g.setColor( saved );
 
        if( backgroundImage != null )
        {
            int imageX = ( getWidth() - backgroundImage.getWidth( this ) ) / 2;
            int imageY = ( getHeight() - backgroundImage.getHeight( this ) ) / 2;
            g.drawImage( backgroundImage, imageX, imageY, this );
        }
 
        if( !getText().equals( "" ) )
        {
            g.drawString( super.getText(), getWidth() / 2, getHeight() / 2  );
        }
 
        if( getIcon() != null )
        {
            Icon icon = getIcon();
            icon.paintIcon( this, g, 10, 10 );
        }
    }
}
/*----------------Version detail----------------------------

Modified time: 13:00_11/04/2012

Editor: Lam Kit Yung

Content: 

Create new MapNode class
Add Function:
public int getIndex();

-----------------------------------------------------------
*/
